import { createRouter, createWebHistory } from 'vue-router'

import Layout from '@/layout/index.vue'

/**
 * Note: 路由配置项
 *
 * hidden: true                     // 当设置 true 的时候该路由不会再侧边栏出现 如401，login等页面，或者如一些编辑页面/edit/1
 * alwaysShow: true                 // 当你一个路由下面的 children 声明的路由大于1个时，自动会变成嵌套的模式--如组件页面
 *                                  // 只有一个时，会将那个子路由当做根路由显示在侧边栏--如引导页面
 *                                  // 若你想不管路由下面的 children 声明的个数都显示你的根路由
 *                                  // 你可以设置 alwaysShow: true，这样它就会忽略之前定义的规则，一直显示根路由
 * redirect: noRedirect             // 当设置 noRedirect 的时候该路由在面包屑导航中不可被点击
 * name:'router-name'               // 设定路由的名字，一定要填写不然使用<keep-alive>时会出现各种问题
 * query: '{"id": 1, "name": "ry"}' // 访问路由的默认传递参数
 * roles: ['admin', 'common']       // 访问路由的角色权限
 * permissions: ['a:a:a', 'b:b:b']  // 访问路由的菜单权限
 * meta : {
    noCache: true                   // 如果设置为true，则不会被 <keep-alive> 缓存(默认 false)
    title: 'title'                  // 设置该路由在侧边栏和面包屑中展示的名字
    icon: 'svg-name'                // 设置该路由的图标，对应路径src/assets/icons/svg
    breadcrumb: false               // 如果设置为false，则不会在breadcrumb面包屑中显示
    activeMenu: '/system/user'      // 当路由设置了该属性，则会高亮相对应的侧边栏。
  }
 */
//菜单图标
import ico_m1 from '../assets/img/ico_m1.png';
// import ico_m2 from '../assets/img/ico_m2.png';
// import ico_m3 from '../assets/img/ico_m3.png';
// import ico_m4 from '../assets/img/ico_m4.png';
// import ico_m5 from '../assets/img/ico_m5.png';
// import ico_m6 from '../assets/img/ico_m6.png';
// import ico_m7 from '../assets/img/ico_m7.png';
// import ico_m8 from '../assets/img/ico_m8.png';
// import ico_m9 from '../assets/img/ico_m9.png';
// import ico_m10 from '../assets/img/ico_m10.png';
import { routerList, usePermissionStore } from './getAutoUrl';
import { getToken } from '@/utils/auth';
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '',
      component: Layout,
      redirect: '/index',
      // name: "index",
      children: [
        {
          path: '/index',
          component: () => import('@/views/home.vue'),
          name: 'homeIndex',
          meta: { title: '首页', breadcrumb: true, icon: ico_m1, activeMenu: '/index' }
        },
        // {
        //     path: '/coalStatistics',
        //     component: () => import('@/views/coal/statistics.vue'),
        //     name: 'CoalStatistics',
        //     meta: { title: '煤场概览', breadcrumb: true, icon: ico_m2, activeMenu: '/coalStatistics'},
        // },
        // //料场监控
        // {
        //     path: '/stock',
        //     redirect: 'noRedirect',
        //     meta: { title: '料场监控', breadcrumb: true, icon: ico_m4},
        //     children: [
        //         {
        //             path: '/stock/record',
        //             component: () => import('@/views/stock/record.vue'),
        //             name: 'Stock',
        //             meta: { title: '盘煤记录', breadcrumb: false},
        //         },
        //         {
        //             path: '',
        //             redirect: '/stock/record',
        //             meta: { title: '盘煤记录', breadcrumb: false, hidden: true},
        //             children: [
        //                 {
        //                     path: '/stock/record/info',
        //                     component: () => import('@/views/stock/recordInfo.vue'),
        //                     name: 'stockRecordInfocoal',
        //                     meta: { title: '详情', breadcrumb: false},
        //                 },
        //                 {
        //                     path: '/stock/record/report',
        //                     component: () => import('@/views/stock/report.vue'),
        //                     name: 'CoalRecordReport',
        //                     meta: { title: '检测报告', breadcrumb: true},
        //                 }
        //             ]
        //         },
        //         {
        //             path: '',
        //             redirect: 'noRedirect',
        //             meta: { title: '煤场监测', breadcrumb: true},
        //             children: [
        //                 {
        //                     path: '/stock/measure',
        //                     component: () => import('@/views/stock/measure.vue'),
        //                     name: 'Measure',
        //                     meta: { title: '三维动态测量', breadcrumb: true},
        //                 },
        //                 {
        //                     path: '/stock/yard3d',
        //                     component: () => import('@/views/stock/yard3d.vue'),
        //                     name: 'StockYard3d',
        //                     meta: { title: '三维煤场1', breadcrumb: false},
        //                 },
        //                 {
        //                     path: '/stock/yard',
        //                     component: () => import('@/views/stock/yard.vue'),
        //                     name: 'StockYard',
        //                     meta: { title: '三维煤场', breadcrumb: false},
        //                 },
        //                 {
        //                     path: '/stock/coalPile',
        //                     component: () => import('@/views/stock/coalPile.vue'),
        //                     name: 'StockCoalPile',
        //                     meta: { title: '煤堆信息', breadcrumb: false},
        //                 },
        //                 {
        //                     path: '/stock/coalInfo',
        //                     component: () => import('@/views/stock/coalInfo.vue'),
        //                     name: 'StockCoalInfo',
        //                     meta: { title: '煤堆一览表', breadcrumb: false},
        //                 },
        //                 {
        //                     path: '/stock/environment',
        //                     component: () => import('@/views/stock/environment.vue'),
        //                     name: 'Environment',
        //                     meta: { title: '环境监测', breadcrumb: false},
        //                 },
        //                 {
        //                     path: '/stock/digital',
        //                     component: () => import('@/views/stock/digital.vue'),
        //                     name: 'Digital',
        //                     meta: { title: '数字化煤场', breadcrumb: false},
        //                 },
        //             ]
        //         },
        //         {
        //             path: '/stock/video',
        //             component: () => import('@/views/stock/video.vue'),
        //             name: 'StockVideo',
        //             meta: { title: '视频在线', breadcrumb: false, activeMenu: '/stock/video'},
        //         },
        //         {
        //             path: '/stock/videoInfo',
        //             meta: { title: '视频在线', breadcrumb: false, hidden: true},
        //             component: () => import('@/views/stock/videoInfo.vue'),
        //         },
        //         {
        //             path: '/stock/alarm',
        //             component: () => import('@/views/stock/alarm.vue'),
        //             name: 'StockAlarm',
        //             meta: { title: '告警处理', breadcrumb: false, activeMenu: '/stock/alarm'},
        //         },
        //     ]
        // },
        // //入场管理
        // {
        //     path: '/entrance',
        //     redirect: 'noRedirect',
        //     meta: { title: '入场管理', breadcrumb: true, icon: ico_m5},
        //     children: [
        //         {
        //             path: '/entrance/plan',
        //             component: () => import('@/views/entrance/plan.vue'),
        //             name: 'EntrancePlan',
        //             meta: { title: '来煤计划', breadcrumb: true},
        //         },
        //         {
        //             path: '/entrance/quality',
        //             component: () => import('@/views/entrance/quality.vue'),
        //             name: 'EntranceQuality',
        //             meta: { title: '来煤煤质', breadcrumb: true},
        //         },
        //         {
        //             path: '/entrance/record',
        //             component: () => import('@/views/entrance/record.vue'),
        //             name: 'EntranceRecord',
        //             meta: { title: '来煤记录', breadcrumb: false},
        //             children: [
        //                 {
        //                     path: '/entrance/record/report',
        //                     component: () => import('@/views/entrance/report.vue'),
        //                     name: 'EntranceReport',
        //                     meta: { title: '检测报告', breadcrumb: false},
        //                 }
        //             ]
        //         },
        //         {
        //             path: '/entrance/scale',
        //             component: () => import('@/views/entrance/scale.vue'),
        //             name: 'EntranceScale',
        //             meta: { title: '皮带秤记录', breadcrumb: true},
        //         },
        //     ]
        // },
        // //入炉管理
        // {
        //     path: '/furnacee',
        //     redirect: 'noRedirect',
        //     meta: { title: '入炉管理', breadcrumb: true, icon: ico_m6},
        //     children: [
        //         {
        //             path: '/furnace/quality',
        //             component: () => import('@/views/furnace/quality.vue'),
        //             name: 'FurnaceQuality',
        //             meta: { title: '入炉煤质', breadcrumb: false},
        //         },
        //         {
        //             path: '',
        //             redirect: '/furnace/quality',
        //             meta: { title: '入炉煤质', breadcrumb: false, hidden: true},
        //             children: [
        //                 {
        //                     path: '/furnace/quality/export',
        //                     component: () => import('@/views/furnace/export.vue'),
        //                     name: 'FurnaceeExport',
        //                     meta: { title: '导出', breadcrumb: false, activeMenu:"/furnace/quality"}
        //                 }
        //             ]
        //         },
        //         {
        //             path: '/furnaceeRecord',
        //             component: () => import('@/views/furnace/record.vue'),
        //             name: 'FurnaceRecord',
        //             meta: { title: '入炉记录', breadcrumb: true}
        //         },
        //         {
        //             path: '/furnaceeScale',
        //             component: () => import('@/views/furnace/scale.vue'),
        //             name: 'FurnaceScale',
        //             meta: { title: '皮带秤记录', breadcrumb: true}
        //         },
        //     ]
        // },

        // //设施设备
        // {
        //     path: '/device',
        //     redirect: 'noRedirect',
        //     meta: { title: '设施设备', breadcrumb: true, icon: ico_m7},
        //     children: [
        //         {
        //             path: '/device/control',
        //             component: () => import('@/views/device/control.vue'),
        //             name: 'DeviceControl',
        //             meta: { title: '输煤程控', breadcrumb: true},
        //             children: []
        //         },
        //         {
        //             path: '/device/manage',
        //             component: () => import('@/views/device/manage.vue'),
        //             name: 'DeviceManage',
        //             meta: { title: '设备管理', breadcrumb: true},
        //             children: []
        //         },
        //         {
        //             path: '/device/warningList',
        //             component: () => import('@/views/device/warningList.vue'),
        //             name: 'DeviceWarningList',
        //             meta: { title: '设备报警', breadcrumb: true},
        //             children: []
        //         },
        //         {
        //             path: '',
        //             redirect: '/device/warningList',
        //             meta: { title: '设备报警', breadcrumb: false, hidden: true},
        //             children:[
        //                 {
        //                     path: '/device/warningList/info',
        //                     component: () => import('@/views/device/warning.vue'),
        //                     name: 'VideoInfo',
        //                     meta: { title: '视频详情', breadcrumb: false, activeMenu: '/device/warningList'},
        //                 },
        //             ]
        //         },

        //         // {
        //         //     path: '/device/warning',
        //         //     component: () => import('@/views/device/warning.vue'),
        //         //     name: 'DeviceWarning',
        //         //     meta: { title: '设备报警', breadcrumb: true},
        //         //     children: []
        //         // },
        //     ]
        // },
        // //统计分析
        // {
        //     path: '/statistic',
        //     redirect: 'noRedirect',
        //     meta: { title: '统计分析', breadcrumb: true, icon: ico_m8},
        //     children: [
        //         {
        //             path: '/statistic/coalArrival',
        //             component: () => import('@/views/statistic/coalArrival.vue'),
        //             name: 'CoalArrival',
        //             meta: { title: '来煤统计', breadcrumb: true},
        //             children: []
        //         },
        //         {
        //             path: '/statistic/furnace',
        //             component: () => import('@/views/statistic/furnace.vue'),
        //             name: 'Furnace',
        //             meta: { title: '入炉统计', breadcrumb: true},
        //             children: []
        //         },
        //         {
        //             path: '/statistic/checkAnalyse',
        //             component: () => import('@/views/statistic/checkAnalyse.vue'),
        //             name: 'CheckAnalyse',
        //             meta: { title: '盘煤结果分析', breadcrumb: true},
        //             children: []
        //         },
        //         {
        //             path: '/statistic/checkStatistic',
        //             component: () => import('@/views/statistic/checkStatistic.vue'),
        //             name: 'CheckStatistic',
        //             meta: { title: '盘煤统计', breadcrumb: true},
        //             children: []
        //         },
        //         {
        //             path: '/statistic/device',
        //             component: () => import('@/views/statistic/device.vue'),
        //             name: 'Device',
        //             meta: { title: '设备统计', breadcrumb: true},
        //             children: []
        //         },
        //     ]
        // },
        // //系统管理
        // {
        //     path: '/system',
        //     redirect: 'noRedirect',
        //     meta: { title: '系统管理', breadcrumb: true,icon: ico_m9}, 
        //     children: [
        //         {
        //             path: '/systemYard',
        //             component: () => import('@/views/system/yard.vue'),
        //             name: 'SystemYard',
        //             meta: { title: '煤场管理', breadcrumb: true},
        //             children: []
        //         },
        //         {
        //             path: '/systemZone',
        //             component: () => import('@/views/system/zone.vue'),
        //             name: 'SystemZone',
        //             meta: { title: '煤场分区', breadcrumb: true},
        //             children: []
        //         },
        //         {
        //             path: '/systemType',
        //             component: () => import('@/views/system/type.vue'),
        //             name: 'SystemType',
        //             meta: { title: '煤种管理', breadcrumb: true},
        //             children: []
        //         },
        //         {
        //             path: '/systemBoiler',
        //             component: () => import('@/views/system/boiler.vue'),
        //             name: 'systemBoiler',
        //             meta: { title: '锅炉管理', breadcrumb: true},
        //             children: []
        //         },
        //         {
        //             path: '/systemScale',
        //             component: () => import('@/views/system/scale.vue'),
        //             name: 'SystemScale',
        //             meta: { title: '皮带秤管理', breadcrumb: true},
        //             children: []
        //         },
        //         {
        //             path: '/systemSupplier',
        //             component: () => import('@/views/system/supplier.vue'),
        //             name: 'SystemSupplier',
        //             meta: { title: '供应商管理', breadcrumb: true},
        //         },
        //     ]
        // },

      ]
    },
    {
      path: '/login',
      component: () => import('@/views/login.vue'),
      name: 'Login',
      meta: { title: '登录', breadcrumb: false, activeMenu: '/login' }
    },
  ]
})

const whiteList = ['/login']
async function autoUrl() {
  const accessRoutes = await usePermissionStore().generateRoutes()
  // console.log(111111, accessRoutes, router);
  // 根据roles权限生成可访问的路由表
  accessRoutes.forEach((route) => {
    const pathName = `${(route.name as string) || (route.path as string).replace(/^\//g, "")}Parent`;
    router.addRoute({ name: pathName, path: route.path, component: Layout });
    router.addRoute(pathName, route) // 动态添加可访问路由表
  })
}
if (getToken() && !routerList.length) {
  await autoUrl();
}

router.beforeEach(async (to, from, next) => {
  if (getToken()) {
    /* has token*/
    if (to.path === '/login') {
      next({ path: '/' })
    } else {
      if (!routerList.length) {
        await autoUrl();
        next()
      } else {
        next()
      }
    }
  } else {
    // 没有token
    if (whiteList.indexOf(to.path) !== -1) {
      // 在免登录白名单，直接进入
      next()
    } else {
      next(`/login`) // 否则全部重定向到登录页
      // next(`/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页
    }
  }
})

router.afterEach(() => {
  // NProgress.done()
})
export default router
